package com.ysy.logistics.service.system.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.echarts.PieTotal;
import com.ysy.logistics.entities.system.SysRole;
import com.ysy.logistics.entities.system.UserRole;
import com.ysy.logistics.mapper.system.SysRoleMapper;
import com.ysy.logistics.mapper.system.UserRoleMapper;
import com.ysy.logistics.service.system.UserRoleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/13 9:49
 **/
@Slf4j
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Override
    public boolean setROle(String userId, List<String> roleIds) {
        if (StringUtils.isBlank(userId)){
            return false;
        }
        //删除角色权限
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId,userId);
        userRoleMapper.delete(lqw);
        if (CollUtil.isNotEmpty(roleIds)){
            List<UserRole> userRoles = roleIds.stream().map(roleId -> {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                return userRole;
            }).collect(Collectors.toList());
            userRoleMapper.insert(userRoles);
        }
        return true;
    }

    @Override
    public List<PieTotal> userRoleTotal() {
        List<PieTotal> pieTotals = new ArrayList<>();
        //查询出角色
        List<SysRole> sysRoleList = sysRoleMapper.getAll();

        //统计用户根据角色ID分组
       List<Map<String,Object>> result = userRoleMapper.totalByRoleIdGroup();

       if (CollectionUtil.isNotEmpty(sysRoleList)){
           for (int i = 0; i < sysRoleList.size(); i++) {
               PieTotal pieTotal = new PieTotal();
               pieTotal.setName(sysRoleList.get(i).getName());
               pieTotal.setValue(getListMapValue(sysRoleList.get(i).getId(),result));
               pieTotals.add(pieTotal);
           }
       }
        return pieTotals;
    }

    private Long getListMapValue(String colum,List<Map<String,Object>> result){
        if (CollectionUtil.isEmpty(result)){
            return 0L;
        }
        for (Map<String, Object> item : result) {
            String id = item.get("ID").toString();
            String total = item.get("total").toString();
            if (StringUtils.equals(id, colum)) {
                try {
                    return Long.valueOf(total);
                } catch (NumberFormatException e) {
                    return 0L;
                }
            }
        }


        return 0L;
    }
}
